﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CCWin;
using CCWin.Win32;
using CCWin.Win32.Const;
using CCWin.SkinControl;
using GroupChat2013.Common;
using GroupChat2013.Entity;
using GroupChat2013.Logic;

namespace GroupChat2013.Main
{
    public partial class FrmInformation : CCSkinMain
    {
        private List<InformationItem> InformationItems;

        private Color outColor = SystemColors.InfoText;
        private Color inColor = SystemColors.HotTrack;

        private ParseConfig<ClientConfigInfo> parseConfig;

        public FrmInformation()
        {
            InitializeComponent();

            InformationItems = new List<InformationItem>();
            slNext.Click += new EventHandler(slNext_Click);
            slContent.Click += new EventHandler(slContent_Click);

            slContent.MouseEnter += new EventHandler(slContent_MouseEnter);
            slContent.MouseLeave += new EventHandler(slContent_MouseLeave);

            this.FormClosing += new FormClosingEventHandler(FrmInformation_FormClosing);
            this.FormClosed += new FormClosedEventHandler(FrmInformation_FormClosed);
        }

        private void FrmInformation_FormClosed(object sender, FormClosedEventArgs e)
        {

        }

        private void FrmInformation_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (InformationItems.Count > 0)
            {
                slNext_Click(slNext, null);
                e.Cancel = true;
            }
            else
            {
                e.Cancel = false;
            }
        }

        //窗口加载时
        private void FrmInformation_Load(object sender, EventArgs e)
        {
            //初始化窗口出现位置
            Point p = new Point(Screen.PrimaryScreen.WorkingArea.Width - this.Width - 5, Screen.PrimaryScreen.WorkingArea.Height - this.Height);
            this.PointToScreen(p);
            this.Location = p;

            //渐变效果
            tmClose = new System.Timers.Timer();
            tmClose.Elapsed += new System.Timers.ElapsedEventHandler(tmShow_Elapsed);
            tmClose.Interval = 10;

            parseConfig = FrmClientConfig.GetClientConfigInfo();

            if (parseConfig.Parse().NoticeFormConfig.ShowTime > 0)
            {
                timShow.Enabled = true;
                tmClose.Interval = parseConfig.Parse().NoticeFormConfig.ShowTime * 1000;
            }

            NativeMethods.AnimateWindow(this.Handle, 130, AW.AW_SLIDE + AW.AW_VER_NEGATIVE);//开始窗体动画
        }

        private void slNext_Click(object sender, EventArgs e)
        {
            if (InformationItems.Count > 0)
            {
                InformationItems.RemoveAt(0);
                SetView();
            }
        }

        private void slContent_MouseLeave(object sender, EventArgs e)
        {
            var item = slContent.Tag as InformationItem;
            if (item != null && !string.IsNullOrEmpty(item.Url))
            {
                slContent.ForeColor = outColor;
            }
        }

        private void slContent_MouseEnter(object sender, EventArgs e)
        {
            var item = slContent.Tag as InformationItem;
            if (item != null && !string.IsNullOrEmpty(item.Url))
            {
                slContent.ForeColor = inColor;
            }
        }

        private void slContent_Click(object sender, EventArgs e)
        {
            var item = slContent.Tag as InformationItem;
            if (item != null && !string.IsNullOrEmpty(item.Url))
            {
                if (FrmGroup.OpenWithChrome(item.Url))
                {
                    if (item.CallBack != null)
                        item.CallBack();

                    if (InformationItems.Count > 0)
                    {
                        InformationItems.RemoveAt(0);
                        SetView();
                    }
                }
            }
        }

        public void AddInformationItem(InformationItem item)
        {
            item.Url = GroupChat2013.Common.Helper.AddUrlParams(item.Url, "cid=" + EngineContext.Current.CurrUserInfo.CID);

            InformationItems.Insert(0, item);
            SetView();
        }

        private void SetView()
        {
            this.BeginInvoke(new Action(() =>
            {
                if (InformationItems.Count == 0)
                {
                    slContent.Text = string.Empty;
                    slNext.Visible = false;
                    this.Hide();

                    return;
                }

                slNext.Visible = InformationItems.Count > 1;
                InformationItem item = InformationItems[0];
                slContent.Text = item.Content;
                slContent.Tag = item;

                if (!string.IsNullOrEmpty(item.Url))
                {
                    slContent.Cursor = System.Windows.Forms.Cursors.Hand;
                    this.slContent.ForeColor = outColor;
                }
                else
                {
                    slContent.Cursor = System.Windows.Forms.Cursors.Default;
                    this.slContent.ForeColor = System.Drawing.SystemColors.InfoText;
                }
            }));
        }

        #region 渐变效果

        //渐变效果
        System.Timers.Timer tmClose;

        void tmShow_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            InformationItems.Clear();
            this.Close();
        }

        #endregion

        #region 总计时器

        //计时器
        private void timShow_Tick(object sender, EventArgs e)
        {
            //鼠标不在窗体内时
            if (!this.Bounds.Contains(Cursor.Position))
            {
                this.BeginInvoke((MethodInvoker)delegate()
                {
                    if (!tmClose.Enabled)
                    {
                        tmClose.Enabled = true;
                        tmClose.Start();
                    }
                });
            }
            else if (this.Bounds.Contains(Cursor.Position))
            {
                if (tmClose.Enabled)
                {
                    tmClose.Stop();
                    tmClose.Enabled = false;
                }
            }
        }

        #endregion
    }

    public class InformationItem
    {
        /// <summary>
        /// 是否是可以点击的连接
        /// </summary>
        public string  Url { get; set; }

        /// <summary>
        /// 显示内容
        /// </summary>
        public string Content { get; set; }

        /// <summary>
        /// 回调执行
        /// </summary>
        public Action CallBack { get; set; }
    }
}
